const app = getApp();
const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    tabs: [{
        name: "index",
        label: "观众参观时间"
      },
      {
        name: "list",
        label: "活动日程表"
      }
    ],
    visit: {
      title: '展览开放时间',
      titleEn: 'Exhibition Opening Dates and Hours',
      date: '',
    },
    TabCur: 'list',
    visitTimeList: [], //参观日程
    searchForm: {
      pageNumber: 1, // 当前页数
      pageSize: 10, // 页面大小
      sort: 'timeStart',
      order: "asc"
    },
    totalPages: 0,
    loading: false, //是否正在加载数据
    list: [],
  },
  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.name
    })
  },
  //获取参观时间数据
  getVisitTimeData: function() {
    let that = this;
    //加载信息
    util.request(api.VISIT_TIME_LIST).then(function(res) {
      if (res.success === true) {
        let data = new Object();
        let dates = [];
        data.visitTimeList = res.result.map((item, index) => {
          let o = {
            date: item.visitDate.replace(/-/g, '/'),
            time: `${item.visitTimeStart} ~ ${item.visitTimeEnd}`
          };
          o.week = that.getWeek(o.date);
          dates.push(Date.parse(item.visitDate));
          return o;
        });
        if (dates.length > 0) {
          that.formatDate(new Date(Math.min(...dates)), new Date(Math.max(...dates)));
        }
        that.setData(data);
      }
    });

  },
  formatDate(ts, te) {
    let date = '';

    let ys = ts.getFullYear(); //开始年份
    let ms = ts.getMonth() + 1; //开始月份
    let ds = ts.getDate(); //月里面的天
    let ye = te.getFullYear(); //开始年份
    let me = te.getMonth() + 1; //开始月份
    let de = te.getDate(); //月里面的天
    if (ys == ye) { //如果开始年份 等于结束年份
      if (ms == me) { //如果月份相同
        if (ds == de) { //如果天数相同
          date = `${ys}年${ms}月${ds}日`;
        } else {
          date = `${ys}年${ms}月${ds}日-${de}日`;
        }
      } else {
        date = `${ys}年${ms}月${ds}日-${me}月${de}日`
      }
    } else {
      date = `${ys}年${ms}月${ds}日-${ye}年${ye}月${de}日`
    }
    this.setData({
        'visit.date':date
    });

  },
  //获取星期
  getWeek(date) {
    let week = new Date(date).getDay();
    switch (week) {
      case 0:
        return '星期日  Sunday';
      case 1:
        return '星期一  Monday';
      case 2:
        return '星期二  Tuesday';
      case 3:
        return '星期三  Wednesday';
      case 4:
        return '星期四  Thursday';
      case 5:
        return '星期五  Friday';
      case 6:
        return '星期六  Saturday';
    }
  },
  //当前页发生改变
  changePage(v) {
    this.setData({
      'searchForm.pageNumber': v
    });
    this.getActivityData();
  },
  //查询方法
  handleSearch() {
    this.setData({
      'searchForm.pageNumber': 1
    });
    this.getActivityData();
  },
  //获取活动数据
  getActivityData: function() {


    let that = this;
    that.setData({
      loading: true
    })
    //加载信息
    util.request(api.ACTIVITY_LIST, that.data.searchForm).then(function(res) {
      if (res.success === true) {
        let data = new Object();
        let list = [];
        if (that.data.searchForm.pageNumber > 1) { //加载更多  追加信息
          list = that.data.list;
        }
        list.push(...res.result.content)
        data.list = list;
        data.totalPages = res.result.totalPages;
        data.loading = false;
        that.setData(data);
      }
    });
  },
  //初始化数据
  getIndexData: function() {
    let that = this;
    this.getVisitTimeData();
    this.getActivityData();
  },
  //活动详情
  goDetail(e) {
    wx.navigateTo({
      url: '/pages/activity/detail/index?id=' + e.currentTarget.dataset.id,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getIndexData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.TabCur === 'list') {
      this.changePage(++this.data.searchForm.pageNumber);
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})